package org.subatu.orm.entity;

import java.util.HashMap;
import java.util.Map;

/**
 * 实体类创造工厂
 *
 * @author 冯晓东
 */
public class EntityBeanFactory {

    /**
     * 缓存所有 实体sql
     */
    private final static Map<String, EntityBean> entitySqlMap = new HashMap<>();

    private static EntityBeanFactory instance = new EntityBeanFactory();

    private EntityBeanFactory() {
    }

    public static EntityBeanFactory getInstance() {
        return instance;
    }

    // 获取实体豆子
    public static EntityBean getBean(Class<?> clazz) {
        EntityBean entityBean = entitySqlMap.get(clazz.getName());
        if (entityBean != null) {
            return entityBean;
        }
        // 解析并设置到缓存
        entityBean = parseEntitySql(clazz);
        entitySqlMap.put(clazz.getName(), entityBean);
        return entityBean;
    }

    /**
     * 解析并设置到缓存
     */
    private static EntityBean parseEntitySql(Class<?> clazz) {
        EntityBean entityBean = new EntityBean(clazz);
        // 执行初始化
        entityBean.init();
        return entityBean;
    }

}
